package com.hiultra.archive.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.hiultra.archive.mapper.ArcShelvesMapper;
import com.hiultra.archive.service.IArcShelvesService;
import com.hiultra.common.core.domain.TreeSelect;
import com.hiultra.common.core.domain.entity.ArcShelves;
import com.hiultra.common.exception.ServiceException;
import com.hiultra.common.utils.ArcUtil;
import com.hiultra.common.utils.DateUtils;
import com.hiultra.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 密集架Service业务层处理
 *
 * @author ls
 * @date 2022-07-28
 */
@Service
public class ArcShelvesServiceImpl implements IArcShelvesService {
    @Autowired
    private ArcShelvesMapper arcShelvesMapper;

    /**
     * 查询密集架
     *
     * @param shelvesId 密集架主键
     * @return 密集架
     */
    @Override
    public ArcShelves selectArcShelvesByShelvesId(Long shelvesId) {
        return arcShelvesMapper.selectArcShelvesByShelvesId(shelvesId);
    }

    /**
     * 查询密集架列表
     *
     * @param arcShelves 密集架
     * @return 密集架
     */
    @Override
    public List<ArcShelves> selectArcShelvesList(ArcShelves arcShelves) {
        return arcShelvesMapper.selectArcShelvesList(arcShelves);
    }

    /**
     * 新增密集架
     *
     * @param arcShelves 密集架
     * @return 结果
     */
    @Override
    public int insertArcShelves(ArcShelves arcShelves) {
        int i = 0;
        arcShelves.setLeve(0);
        arcShelves.setCreateTime(DateUtils.getNowDate());
        //判断是否已经存在
        ArcShelves arcShelvesCheck = arcShelvesMapper.checkInsert(arcShelves);
        if (null == arcShelvesCheck) {
            String shelvesCode = null;
            // 自动生成catalogCode
            // 取出同级最大catalogCode
            String maxCode = arcShelvesMapper.selectMaxCode(arcShelves.getParentId());
            if (ObjectUtil.isEmpty(maxCode)) {
                shelvesCode = "001";
            } else {
                shelvesCode = ArcUtil.get000NextCode(maxCode);
            }

            if (0 == arcShelves.getParentId()) {
                arcShelves.setShelvesCode(shelvesCode);
            } else {
                //查询父级信息
                ArcShelves arcShelves1 = arcShelvesMapper.selectArcShelvesByShelvesId(arcShelves.getParentId());
                arcShelves.setShelvesCode(arcShelves1.getShelvesCode() + shelvesCode);
            }
            arcShelves.setCreateTime(DateUtils.getNowDate());

            i = arcShelvesMapper.insertArcShelves(arcShelves);
        } else {
            throw new ServiceException("存在重复编号，请检查后重试！");
        }
        return i;
    }

    /**
     * 修改密集架
     *
     * @param arcShelves 密集架
     * @return 结果
     */
    @Override
    public int updateArcShelves(ArcShelves arcShelves) {
        int i = 0;
        ArcShelves tempobj = arcShelvesMapper.checkUpdate(arcShelves);
        if (null == tempobj) {
            arcShelves.setUpdateTime(DateUtils.getNowDate());
            i = arcShelvesMapper.updateArcShelves(arcShelves);
        } else {
            throw new ServiceException("数据已存在！");
        }
        return i;
    }

    /**
     * 批量删除密集架
     *
     * @param shelvesIds 需要删除的密集架主键
     * @return 结果
     */
    @Override
    public int deleteArcShelvesByShelvesIds(Long[] shelvesIds) {
        //删除下级
        arcShelvesMapper.deleteArcShelvesAllInfo(shelvesIds);
        return arcShelvesMapper.deleteArcShelvesByShelvesIds(shelvesIds);
    }

    /**
     * 删除密集架信息
     *
     * @param shelvesId 密集架主键
     * @return 结果
     */
    @Override
    public int deleteArcShelvesByShelvesId(Long shelvesId) {
        return arcShelvesMapper.deleteArcShelvesByShelvesId(shelvesId);
    }

    /**
     * 设置密集架规格
     *
     * @param arcShelves 密集架
     * @return 结果
     */
    @Override
    @Transactional
    public int updateColumnAndSection(ArcShelves arcShelves) {
        Long shelvesId = arcShelves.getShelvesId();
        Integer columnNum = arcShelves.getColumnNum();
        Integer sectionNum = arcShelves.getSectionNum();
        Integer layerNum = arcShelves.getLayerNum();

        // 判断是否已经设置
        // 获取层级
        ArcShelves entityOne = arcShelvesMapper.selectArcShelvesByShelvesId(shelvesId);
        String shelvesCode = entityOne.getShelvesCode();
        Long storehouseId = entityOne.getStorehouseId();
        // 获取当前层设置
        Integer columnNumNow = entityOne.getColumnNum();
        if (null != columnNumNow) {
            arcShelvesMapper.deleteArcShelvesAllInfoOne(shelvesId);
        }

        // 首先添加列
        ArcShelves columnEntity = new ArcShelves();
        ArcShelves leftEntity = new ArcShelves();
        ArcShelves rightEntity = new ArcShelves();
        ArcShelves sectionLEntity = new ArcShelves();
        ArcShelves sectionREntity = new ArcShelves();
        ArcShelves layerLEntity = new ArcShelves();
        ArcShelves layerREntity = new ArcShelves();
        for (int i = 0; i < columnNum; i++) {
            columnEntity.setShelvesCode(shelvesCode + "0" + (i + 1));
            columnEntity.setShelvesName((i + 1) + "列");
            columnEntity.setStorehouseId(storehouseId);
            columnEntity.setParentId(arcShelves.getShelvesId());
            columnEntity.setLeve(1);
            columnEntity.setStatus(0);
            columnEntity.setDelflag(0);
            columnEntity.setZshelvesId(shelvesId);
            arcShelvesMapper.insertArcShelves(columnEntity);

            // 设置左侧
            leftEntity.setShelvesCode(shelvesCode + "0" + (i + 1) + "A");
            leftEntity.setShelvesName("A面");
            leftEntity.setStorehouseId(storehouseId);
            leftEntity.setParentId(columnEntity.getShelvesId());
            leftEntity.setLeve(2);
            leftEntity.setStatus(0);
            leftEntity.setDelflag(0);
            leftEntity.setZshelvesId(shelvesId);
            arcShelvesMapper.insertArcShelves(leftEntity);

            for (int z = 0; z < sectionNum; z++) {
                // 设置左侧节
                sectionLEntity.setShelvesCode(shelvesCode + "0" + (i + 1) + "A0" + (z + 1));
                sectionLEntity.setShelvesName((z + 1) + "节");
                sectionLEntity.setStorehouseId(storehouseId);
                sectionLEntity.setParentId(leftEntity.getShelvesId());
                sectionLEntity.setLeve(3);
                sectionLEntity.setStatus(0);
                sectionLEntity.setDelflag(0);
                sectionLEntity.setZshelvesId(shelvesId);
                arcShelvesMapper.insertArcShelves(sectionLEntity);

                // 设置层
                for (int j = 0; j < layerNum; j++) {
                    layerLEntity.setShelvesCode(shelvesCode + "0" + (i + 1) + "A0" + (z + 1) + "0" + (j + 1));
                    layerLEntity.setShelvesName((j + 1) + "层");
                    layerLEntity.setStorehouseId(storehouseId);
                    layerLEntity.setParentId(sectionLEntity.getShelvesId());
                    layerLEntity.setLeve(4);
                    layerLEntity.setStatus(0);
                    layerLEntity.setDelflag(0);
                    layerLEntity.setZshelvesId(shelvesId);
                    layerLEntity.setRfidCode("300" + layerLEntity.getShelvesCode());
                    arcShelvesMapper.insertArcShelves(layerLEntity);

                }
            }

            // 设置右侧
            rightEntity.setShelvesCode((shelvesCode + "0" + (i + 1) + "B"));
            rightEntity.setShelvesName("B面");
            rightEntity.setStorehouseId(storehouseId);
            rightEntity.setParentId(columnEntity.getShelvesId());
            rightEntity.setLeve(2);
            rightEntity.setStatus(0);
            rightEntity.setDelflag(0);
            rightEntity.setZshelvesId(shelvesId);
            arcShelvesMapper.insertArcShelves(rightEntity);
            for (int z = 0; z < sectionNum; z++) {

                // 设置右侧节
                sectionREntity.setShelvesCode(shelvesCode + "0" + (i + 1) + "B0" + (z + 1));
                sectionREntity.setShelvesName((z + 1) + "节");
                sectionREntity.setStorehouseId(storehouseId);
                sectionREntity.setParentId(rightEntity.getShelvesId());
                sectionREntity.setLeve(3);
                sectionREntity.setStatus(0);
                sectionREntity.setDelflag(0);
                sectionREntity.setZshelvesId(shelvesId);
                arcShelvesMapper.insertArcShelves(sectionREntity);

                // 设置层
                for (int j = 0; j < layerNum; j++) {
                    layerREntity.setShelvesCode(shelvesCode + "0" + (i + 1) + "B0" + (z + 1) + "0" + (j + 1));
                    layerREntity.setShelvesName((j + 1) + "层");
                    layerREntity.setStorehouseId(storehouseId);
                    layerREntity.setParentId(sectionREntity.getShelvesId());
                    layerREntity.setLeve(4);
                    layerREntity.setStatus(0);
                    layerREntity.setDelflag(0);
                    layerREntity.setZshelvesId(shelvesId);
                    layerREntity.setRfidCode("300" + layerREntity.getShelvesCode());
                    arcShelvesMapper.insertArcShelves(layerREntity);
                }
            }
        }
        return arcShelvesMapper.updateArcShelves(arcShelves);
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param arcShelves 密集架列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildShelvesTreeSelect(List<ArcShelves> arcShelves){
        List<ArcShelves> shelvesTrees = buildShelvesTree(arcShelves);
        return shelvesTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param arcShelves 部门列表
     * @return 树结构列表
     */
    public List<ArcShelves> buildShelvesTree(List<ArcShelves> arcShelves)
    {
        List<ArcShelves> returnList = new ArrayList<ArcShelves>();
        List<Long> tempList = new ArrayList<Long>();
        for (ArcShelves shelves : arcShelves)
        {
            tempList.add(shelves.getShelvesId());
        }
        for (ArcShelves shelves : arcShelves)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(shelves.getParentId()))
            {
                recursionFn(arcShelves, shelves);
                returnList.add(shelves);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = arcShelves;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<ArcShelves> list, ArcShelves t)
    {
        // 得到子节点列表
        List<ArcShelves> childList = getChildList(list, t);
        t.setChildren(childList);
        for (ArcShelves tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<ArcShelves> getChildList(List<ArcShelves> list, ArcShelves t)
    {
        List<ArcShelves> tlist = new ArrayList<ArcShelves>();
        Iterator<ArcShelves> it = list.iterator();
        while (it.hasNext())
        {
            ArcShelves n = (ArcShelves) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getShelvesId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<ArcShelves> list, ArcShelves t)
    {
        return getChildList(list, t).size() > 0;
    }


    /**
     * 查询密集架列表- 查询密集架列表-根据等级查询
     *
     * @param arcShelves 密集架
     * @return 密集架集合
     */
    @Override
    public List<ArcShelves> selectArcShelvesByLeve(ArcShelves arcShelves){
        arcShelves.setLeve(4);
        List<ArcShelves> arcShelvesL =arcShelvesMapper.selectArcShelvesByLeve(arcShelves);
        return arcShelvesL;
    }
}
